$(document).ready(function () {
	//new add	
	var graduated_xl = [];
	
	

	

	$(".son_ul2").hide();
	//国家、毕业院校、所在院系、专业4级级联json串
	var ntChoice=[
    	{nationName:"A",graduated:[
    		{graName:"A1",departmt:[{depName:"A11",major:[{majorName:"A111"},{majorName:"A112"}]},{depName:"A12",major:[{majorName:"A121"},{majorName:"A122"}]}]},
    		{graName:"A2",departmt:[{depName:"A21",major:[{majorName:"A211"},{majorName:"A212"}]},{depName:"A22",major:[{majorName:"A221"},{majorName:"A222"}]}]}
    	],
    	education:[{eduName:"Aa"},{eduName:"Ab"},{eduName:"Ac"},{eduName:"Ad"},{eduName:"Ae"}],
    	degree:[{degName:"Aaa"},{degName:"Abb"},{degName:"Acc"},{degName:"Add"},{degName:"Aee"},]
    	},
    	{nationName:"B",graduated:[
    		{graName:"B1",departmt:[{depName:"B11",major:[{majorName:"B111"},{majorName:"B112"}]},{depName:"B12",major:[{majorName:"B121"},{majorName:"B122"}]}]},
    		{graName:"B2",departmt:[{depName:"B21",major:[{majorName:"B211"},{majorName:"B212"}]},{depName:"B22",major:[{majorName:"B221"},{majorName:"B222"}]}]}
    	],
    	education:[{eduName:"Ba"},{eduName:"Bb"},{eduName:"Bc"},{eduName:"Bd"},{eduName:"Be"}],
    	degree:[{degName:"Baa"},{degName:"Bbb"},{degName:"Bcc"},{degName:"Bdd"},{degName:"Bee"},]
    	},
    	{nationName:"C",graduated:[
    		{graName:"C1",departmt:[{depName:"C11",major:[{majorName:"C111"},{majorName:"C112"}]},{depName:"C12",major:[{majorName:"C121"},{majorName:"C122"}]}]},
    		{graName:"C2",departmt:[{depName:"C21",major:[{majorName:"C211"},{majorName:"C212"}]},{depName:"C22",major:[{majorName:"C221"},{majorName:"C222"}]}]}
    	],
    	education:[{eduName:"Ca"},{eduName:"Cb"},{eduName:"Cc"},{eduName:"Cd"},{eduName:"Ce"}],
    	degree:[{degName:"Caa"},{degName:"Cbb"},{degName:"Ccc"},{degName:"Cdd"},{degName:"Cee"},]
    	}
	];
	
	
	
//	失去焦点的时候,也清空一下
	$(".select_input").live('blur',function(){
		assets2_class=$(this).parents(".selectWrap2").attr('class');
		if(assets2_class.indexOf("graduated_xl")>0){
			$(this).parents('.item').find(".departmt_xl .select_input,.major_xl .select_input").val("");
//			
		}else if(assets2_class.indexOf("departmt_xl")>0){
			$(this).parents('.item').find(".major_xl .select_input").val("");
		}

	})
	
	$(".select_input").live('focus',function(){	
		$(this).css('color',"black");
		graduated_xl=[];
		assets2_class=$(this).parents(".selectWrap2").attr('class');
		if(assets2_class.indexOf("departmt_xl")>0){		
			
			var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
			var graName=$(this).parents('.item').find('.graduated_xl .select_input').val();		
			for(var i=0;i<ntChoice.length;i++){
		  		if(ntChoice[i].nationName==nationName){
		  			for(var k=0;k<ntChoice[i].graduated.length;k++){
		  					if(ntChoice[i].graduated[k].graName==graName){
		  						for(var n=0;n<ntChoice[i].graduated[k].departmt.length;n++){
		  							var departmtHtml='<li>'+ntChoice[i].graduated[k].departmt[n].depName+'</li>';
									$(this).find('.son_ul2').append(departmtHtml);  
									graduated_xl.push(ntChoice[i].graduated[k].departmt[n].depName);
		  						}
		  					}
		  			}
		  		}
			}
			$(".select_input").autocomplete({
				source: graduated_xl,
			});
			$(".son_ul2").hide();		
		}else if(assets2_class.indexOf("major_xl")>0){
			
			var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();//国家名字
			var graName=$(this).parents('.item').find('.graduated_xl .select_input').val();//毕业院校名字	
			var depName=$(this).parents('.item').find('.departmt_xl .select_input').val();//所在院系名字	
			for(var i=0;i<ntChoice.length;i++){
		  		if(ntChoice[i].nationName==nationName){
		  			for(var k=0;k<ntChoice[i].graduated.length;k++){
		  					if(ntChoice[i].graduated[k].graName==graName){
		  						for(var n=0;n<ntChoice[i].graduated[k].departmt.length;n++){
		  							if(ntChoice[i].graduated[k].departmt[n].depName==depName){
		  								for(var m=0;m<ntChoice[i].graduated[k].departmt[n].major.length;m++){
		  									var departmtHtml='<li>'+ntChoice[i].graduated[k].departmt[n].major[m].majorName+'</li>';
											$(this).find('.son_ul2').append(departmtHtml);  
											graduated_xl.push(ntChoice[i].graduated[k].departmt[n].major[m].majorName);
		  								}
		  							}
		  							  
		  						}
		  					}
		  			}
		  		}
			}
			$(".select_input").autocomplete({
				source: graduated_xl,
			});
			$(".son_ul2").hide();	
		}else if(assets2_class.indexOf("graduated_xl")>0){
//			说明是获取的毕业院校的焦点			
		  	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
		  	for(var j in ntChoice){
		  		if(ntChoice[j].nationName==nationName){
		  			for(var k=0;k<ntChoice[j].graduated.length;k++){
	  					var provinceHtml='<li>'+ntChoice[j].graduated[k].graName+'</li>';
	  					$(this).find('.son_ul2').append(provinceHtml); 
	  					graduated_xl.push(ntChoice[j].graduated[k].graName);
		  			}
		  		}
		  	}
			$(".select_input").autocomplete({
				source: graduated_xl,
			});
			$(".son_ul2").hide();	
		}

	})
	
	var assets2_class;//获取点击的父元素的class
	
//	可以下拉可以填写的按钮点击下拉
	$(".select_xlbtn,.select_box2 span").live('click',function() {		
		assets2_class=$(this).parents('.selectWrap2').attr('class');
		$(this).parents('.selectWrap2').find('.emptytips').remove();
		var self=$(this);
		//如果是选择的国家,随便选
		if(assets2_class.indexOf("nationality_xl")>0){
			canClick2(self);	
		}else if(assets2_class.indexOf("graduated_xl")>0){
//			选择毕业院校
			if(self.parents('.item').find(".nationality_xl li span").text()!=""){
				canClick2(self);
			}
		}else if(assets2_class.indexOf("departmt_xl")>0){
//			选择所在院系
			var gra_add=self.parents('.item').find(".graduated_xl li .select_input").val();
			var gracanxl_add=false;
			self.parents('.item').find(".graduated_xl li .son_ul2 li").each(function(){
				if($(this).text()==gra_add){
					gracanxl_add=true;
				}
			})
			if(gracanxl_add){
				canClick2(self);
			}
		}else if(assets2_class.indexOf("major_xl")>0){
//			选择所在专业
			var dep_add=self.parents('.item').find(".departmt_xl li .select_input").val();
			var depcanxl_add=false;
			self.parents('.item').find(".departmt_xl li .son_ul2 li").each(function(){
				if($(this).text()==dep_add){
					depcanxl_add=true;
				}
			})
			if(depcanxl_add){
				canClick2(self);
			}
		}else if(assets2_class.indexOf("education_xl")>0){
//			选择学历
			if(self.parents('.item').find(".nationality_xl li span").text()!=""){
				canClick2(self);
			}
		}else if(assets2_class.indexOf("degree_xl")>0){
//			选择学位
			if(self.parents('.item').find(".nationality_xl li span").text()!=""){
				canClick2(self);
			}
		}
				
	})

//国家->毕业院校->所在院系->专业四级联调->下拉框中存放数据---start
// 国家选择
$(".nationality_xl").live('click',function(){
	$(this).parents('.item').find(".education_xl .son_ul2,.degree_xl .son_ul2").hide();
	$(this).find('.son_ul2').empty();
	for(var i in ntChoice){
		var countryHtml='<li>'+ntChoice[i].nationName+'</li>';
		$(this).find('.son_ul2').append(countryHtml);
	}
})
//毕业院校选择
$(".graduated_xl").live('click',function(){
  	$(this).find('.son_ul2').empty();
  	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
  	for(var j in ntChoice){
  		if(ntChoice[j].nationName==nationName){
  			for(var k=0;k<ntChoice[j].graduated.length;k++){
  					var provinceHtml='<li>'+ntChoice[j].graduated[k].graName+'</li>';
  					$(this).find('.son_ul2').append(provinceHtml); 
  					
  			}
  		}
  	}
})
//所在院系选择
$(".departmt_xl").live('click',function(){
	$(this).find('.son_ul2').empty();
	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
	var graName=$(this).parents('.item').find('.graduated_xl .select_input').val();		
	for(var i=0;i<ntChoice.length;i++){
  		if(ntChoice[i].nationName==nationName){
  			for(var k=0;k<ntChoice[i].graduated.length;k++){
  					if(ntChoice[i].graduated[k].graName==graName){
  						for(var n=0;n<ntChoice[i].graduated[k].departmt.length;n++){
  							var departmtHtml='<li>'+ntChoice[i].graduated[k].departmt[n].depName+'</li>';
							$(this).find('.son_ul2').append(departmtHtml);     
  						}
  					}
  			}
  		}
	}
})
//所在专业选择
$(".major_xl").live('click',function(){
	$(this).find('.son_ul2').empty();
	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();//国家名字
	var graName=$(this).parents('.item').find('.graduated_xl .select_input').val();//毕业院校名字	
	var depName=$(this).parents('.item').find('.departmt_xl .select_input').val();//所在院系名字	
	for(var i=0;i<ntChoice.length;i++){
  		if(ntChoice[i].nationName==nationName){
  			for(var k=0;k<ntChoice[i].graduated.length;k++){
  					if(ntChoice[i].graduated[k].graName==graName){
  						for(var n=0;n<ntChoice[i].graduated[k].departmt.length;n++){
  							if(ntChoice[i].graduated[k].departmt[n].depName==depName){
  								for(var m=0;m<ntChoice[i].graduated[k].departmt[n].major.length;m++){
  									var departmtHtml='<li>'+ntChoice[i].graduated[k].departmt[n].major[m].majorName+'</li>';
									$(this).find('.son_ul2').append(departmtHtml);   
  								}
  							}
  							  
  						}
  					}
  			}
  		}
	}
})
//国家->毕业院校->所在院系->专业四级联调,下拉框中存放数据---end




//国家->学历
$(".education_xl").live('click',function(){
  	$(this).find('.son_ul2').empty();
  	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
  	for(var j in ntChoice){
  		if(ntChoice[j].nationName==nationName){
  			for(var k=0;k<ntChoice[j].education.length;k++){
  					var eduHtml='<li>'+ntChoice[j].education[k].eduName+'</li>';
  					$(this).find('.son_ul2').append(eduHtml);      				
  			}
  		}
  	}
})

//国家->学位
$(".degree_xl").live('click',function(){
  	$(this).find('.son_ul2').empty();
  	var nationName=$(this).parents('.item').find('.nationality_xl .select_box2 span').text();
  	for(var j in ntChoice){
  		if(ntChoice[j].nationName==nationName){
  			for(var k=0;k<ntChoice[j].degree.length;k++){
  					var degreeHtml='<li>'+ntChoice[j].degree[k].degName+'</li>';
  					$(this).find('.son_ul2').append(degreeHtml);      				
  			}
  		}
  	}
})

$('ul .son_ul2 li').live('click', function() {	
	$(this).parents('.item').find(".nationality_xl span").css('color',"black");
	$(this).parents('.item').find(".education_xl span").css('color',"black")
	$(this).parents('.item').find(".degree_xl span").css('color',"black")
	assets2_class=$(this).parents('.selectWrap2').attr('class');
	
	var graduated_xl=$(this).parents('.item').find(".graduated_xl .select_input");
	var departmt_xl=$(this).parents('.item').find(".departmt_xl .select_input");
	var major_xl=$(this).parents('.item').find(".major_xl .select_input");
	
	if(assets2_class.indexOf('nationality_xl')>0){
//		当不支持placeholder属性的时候
		 if (!supportPlaceholder) {		 	
	    	graduated_xl.val("毕业院校");
	    	departmt_xl.val("院系");
	    	major_xl.val("专业");	  
	    }else{
	    	graduated_xl.val("");
	    	departmt_xl.val("");
	    	major_xl.val("");
	    }
	    graduated_xl.css('color','#666');
    	departmt_xl.css('color','#666');
    	major_xl.css('color','#666');

		var education_xl=$(this).parents('.item').find(".education_xl span");
			if($(this).parents('.item').find(".education_xl").attr('class')=='selectWrap2 education_xl eduadd_lyc'){
				if(education_xl.text()!="学历"){
					education_xl.text("学历");
					education_xl.css('color','#666');
				}else if(education_xl.text()=="学历"){
					education_xl.css('color','#666');
				}
			}
		 	else{
				$(this).parents('.item').find(".education_xl li span,.degree_xl li span").empty();
			}	

	}else if(assets2_class.indexOf('graduated_xl')>0){
		graduated_xl.css('color','black');
		if (!supportPlaceholder) {
	    	departmt_xl.val("院系");
	    	major_xl.val("专业");
	    	
	   }else{
	    	departmt_xl.val("");
	    	major_xl.val("");
	   }
	   departmt_xl.css('color','#666');
    	major_xl.css('color','#666');
	}else if(assets2_class.indexOf('departmt_xl')>0){
		departmt_xl.css('color','black');
		if (!supportPlaceholder) {
	    	major_xl.val("专业");
	    	
	   }else{
	    	major_xl.val("");
	   }
	   major_xl.css('color','#666');
	}else if(assets2_class.indexOf('major_xl')>0){
		major_xl.css('color','black');
	}
//放进input里面value值
	$(this).parents('.selectWrap2').find('.select_input').val($(this).html());
//选择的值放进span标签里面
	$(this).parents('.selectWrap2 li').find('span').html($(this).html());				
	$(this).parents('.selectWrap2 li').find('ul').slideUp(10);
	$(this).parents('.selectWrap2 li').css({
		"zIndex": "0"
	});
});
	
function canClick2(self) {
	self.parent().css({"zIndex": "555"})
	self.parent().find('ul.son_ul2').slideDown();
	self.parent().find('li').hover(function() {
		self.addClass('hover')
	}, function() {
		self.removeClass('hover')
	});
	self.parent().hover(function() {},
		function() {
			self.parent().find("ul .son_ul2").slideUp(10);
			self.parent().find("li .select_box2").css({
				"zIndex": "0"
			})
		}
	);
}


 //placeholder
    supportPlaceholder = 'placeholder'in document.createElement('input'),
        placeholder = function (input) {

            var text = input.attr('placeholder'),
            defaultValue = input.defaultValue;

            if (!defaultValue) {           	
                input.val(text);
                input.addClass("phcolor");
                input.css('color','#6d6d6d');
            }
            input.focus(function () {
                if (input.val() == text) {
                    $(this).val("");
                }
            });
            input.blur(function () {
                if (input.val() == "") {
                    $(this).val(text).addClass("phcolor");
                    input.css('color','#6d6d6d');
                }
            });
            //输入的字符不为灰色
            input.keydown(function () {
                $(this).removeClass("phcolor");
                input.css('color','black');
            });
        };

    //当浏览器不支持placeholder属性时，调用placeholder函数
    if (!supportPlaceholder) {
        $('input,textarea').each(function () {
            if ($(this).attr("type") == "text") {

                placeholder($(this));
            }
        });
    }

})